import Vue from 'vue'
import Vuex from 'vuex'
import homepage from "./homepage"
import {cityApi} from "@api/request"
import detail from "./detail"

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    cityList:localStorage.getItem("cityList")?JSON.parse(localStorage.getItem("cityList")):[],
    hotCity:localStorage.getItem("hotCity")?JSON.parse(localStorage.getItem("hotCity")):[],
  },
  mutations: {
    handleCityList(state,data){
      var cityList = [];
      var hotCity = [];
      for(var i=0;i<data[0].cityEntry.length;i++){
        hotCity.push(data[0].cityEntry[i]);
      }
      
      for(var i=1;i<data.length;i++){
        cityList.push(data[i])
      }
      console.log(cityList)
      console.log(hotCity)
      localStorage.setItem("cityList",JSON.stringify(cityList));
      localStorage.setItem("hotCity",JSON.stringify(hotCity))
 
    }
  },
  actions: {
    async handleGetCityList({commit}){
      let data =await cityApi();
      commit("handleCityList",data.result);
      
    },
    async userLogin({commit,dispatch},info){
      let data = await userLoginApi(info);
      if(data.code == 200){
        commit("handleLogin",data)
        return true;
      }else{
        return false;
      }
    }
  },
  modules: {
    homepage,
    detail
  }
})
